package com.ican.dao.product;

import java.util.List;

import org.springframework.stereotype.Repository;

import com.ican.common.hibernate.HibernateDao;
import com.ican.domain.product.ProductAttribute;

@Repository("productAttributeDao")
public class ProductAttributeDao extends HibernateDao<ProductAttribute, Integer> {
    
    public List<ProductAttribute> findProductAttr(Integer productId, String attr, String value, Integer langId){
        String hql = "from ProductAttribute where productId = ? and optionName = ? and optionValue = ? and langId = ?";
        return this.find(hql, new Object[]{productId, attr, value, langId});
    }
    
    public List<ProductAttribute> findProductAttrByProductId(Integer productId){
        String hql = "from ProductAttribute where productId = " + productId + " order by nameCode, valueCode";
        return this.find(hql);
    }

    public void updateAttrOptionName(String nameNew, String name, Integer langId){
        String hql = "update ProductAttribute set optionName = '" + nameNew + 
                "' where optionName = '" + name + "' and langId = " + langId + " and nameCode is null";
        this.batchExecute(hql);
    }
    
    public void updateAttrOptionCode(String name, Integer langId, String nameCode){
        String hql = "update ProductAttribute set nameCode = '" + nameCode + 
                "' where optionName = '" + name + "' and langId = " + langId + " and nameCode is null";
        this.batchExecute(hql);
    }
    
    public void updateAttrOptionCode(Integer id, String nameCode){
        String hql = "update ProductAttribute set nameCode = '" + nameCode + 
                "' where id = " + id;
        this.batchExecute(hql);
    }
    
    public void updateAttrOptionValueName(String nameCode, String optionValue, Integer langId, String optionValueNew){
        String hql = "update ProductAttribute set optionValue = '" + optionValueNew + 
                "' where nameCode = '" + nameCode + "' and optionValue = '" + optionValue + 
                "' and langId = " + langId + " and valueCode is null";
        this.batchExecute(hql);
    }
    
    public void updateAttrOptionValueCode(String nameCode, String optionValue, String valueCode, Integer langId){
        String hql = "update ProductAttribute set valueCode = '" + valueCode + 
                "' where nameCode = '" + nameCode + "' and optionValue = '" + optionValue + "' and langId = " + langId;
        this.batchExecute(hql);
    }
    
    public void updateAttrOptionValueCode(Integer id, String valueCode){
        String hql = "update ProductAttribute set valueCode = '" + valueCode + 
                "' where id = " + id;
        this.batchExecute(hql);
    }
    
    public void deleteductAttr(String name, Integer langId){
        String hql = "delete from ProductAttribute where optionName = ? and langId = ? and nameCode is null and valueCode is null";
        this.batchExecute(hql, new Object[]{name, langId});
    }
    
    public void deleteductAttr(String nameCode, String optionValue, Integer langId){
        String hql = "delete from ProductAttribute where nameCode = ? and optionValue = ? and langId = ? and valueCode is null";
        this.batchExecute(hql, new Object[]{nameCode, optionValue, langId});
    }
}
